package com.company.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.eduservice.entity.EduCourse;
import com.company.eduservice.feign.OrderFeignService;
import com.company.eduservice.service.EduChapterService;
import com.company.eduservice.service.EduCourseService;
import com.company.eduservice.vo.chapter.ChapterVo;
import com.company.eduservice.vo.front.CourseFrontVo;
import com.company.eduservice.vo.front.CourseWebVo;
import com.company.utils.JwtUtils;
import com.company.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class IndexCourseShow {

    @Resource
    private EduCourseService courseService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private OrderFeignService orderFeignService;

    @PostMapping("/getFrontCourseList/{current}/{limit}")
    @ApiOperation(value = "分页获取课程列表(前端展示，可附带条件)")
    public R pageTeacherList(@PathVariable Long current, @PathVariable Long limit,
                             @RequestBody(required = false) CourseFrontVo frontVo) {
        Page<EduCourse> page = new Page<>(current, limit);
        Map<String, Object> map = courseService.pageTeacherList(page, frontVo);
        return R.ok().data(map);
    }

    //查找课程详情
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //查询基本信息
        CourseWebVo courseWebVo = courseService.getFrontCourseInfo(courseId);
        //查询课程下所有章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        //查询非免费课程下用户观看权限
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse;
        if (StringUtils.isEmpty(memberId)) {
            buyCourse = false;
        } else {
            buyCourse = orderFeignService.isBuyCourse(courseId, memberId);
        }
        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVoList).data("isBuy", buyCourse);
    }

}
